package com.jie.langlangmall.controller;

import com.jie.langlangmall.common.api.CommonPage;
import com.jie.langlangmall.common.api.CommonResult;
import com.jie.langlangmall.common.constant.MessageConstant;
import com.jie.langlangmall.dto.PmsProductCategoryParam;
import com.jie.langlangmall.dto.PmsProductCategoryWithChildrenItem;
import com.jie.langlangmall.model.PmsProductCategory;
import com.jie.langlangmall.service.PmsProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/23
 * @Email 3217998214@qq.com
 * @Description 商品分类管理
 */
@RestController
@Tag(name = "PmsProductCategoryController", description = "商品分类管理")
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService productCategoryService;

    @Operation(summary = "查询所有一级分类及子分类")
    @GetMapping("/list/withChildren")
    public CommonResult<List<PmsProductCategoryWithChildrenItem>> listWithChildren() {
        List<PmsProductCategoryWithChildrenItem> list = productCategoryService.listWithChildren();
        return CommonResult.success(list);
    }


    @Operation(summary = "分页查询")
    @GetMapping("/list/{parentId}")
    public CommonResult<CommonPage<PmsProductCategory>> list(@PathVariable("parentId") Long parentId,
                                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsProductCategory> list = productCategoryService.list(parentId, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @Operation(summary = "根据ID获取商品分类")
    @GetMapping("/{id}")
    public CommonResult<PmsProductCategory> getItem(@PathVariable("id") Long id) {
        PmsProductCategory productCategory = productCategoryService.getItem(id);
        return CommonResult.success(productCategory);
    }

    @Operation(summary = "删除商品分类")
    @PostMapping("/delete/{id}")
    public CommonResult<?> delete(@PathVariable("id") Long id) {
        productCategoryService.delete(id);
        return CommonResult.success();
    }

    @Operation(summary = "修改导航栏状态")
    @PostMapping("/update/navStatus")
    public CommonResult<?> updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer navStatus) {
        if (navStatus == null || (navStatus != 0 && navStatus != 1)) {
            return CommonResult.failed(MessageConstant.SUBMIT_PARAM_ERROR);
        }
        productCategoryService.updateNavStatus(ids, navStatus);
        return CommonResult.success();
    }

    @Operation(summary = "修改显示状态")
    @PostMapping("/update/showStatus")
    public CommonResult<?> updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
        if (showStatus == null || (showStatus != 0 && showStatus != 1)) {
            return CommonResult.failed(MessageConstant.SUBMIT_PARAM_ERROR);
        }
        productCategoryService.updateShowStatus(ids, showStatus);
        return CommonResult.success();
    }

    @Operation(summary = "添加商品分类")
    @PostMapping("/create")
    public CommonResult<?> create(@Validated @RequestBody PmsProductCategoryParam param) {
        productCategoryService.create(param);
        return CommonResult.success();
    }

    @Operation(summary = "修改商品分类")
    @PostMapping("/update/{id}")
    public CommonResult<?> update(@PathVariable("id") Long id,
                                  @Validated @RequestBody PmsProductCategoryParam param) {
        productCategoryService.update(id, param);
        return CommonResult.success();
    }

}
